$(function(){
	$("#updateGdsInfoRemark").bind("click",function(){
		var gdsDesc = $("#gdsDesc").val();
		var url = $webroot + '/upload/uploadHtml';
		if(gdsDesc){
			gdsDesc = escape(gdsDesc);
			$.ajax({
				url: url,
		        data:{
		        	content:gdsDesc
		        },
		        type:"post",
		        dataType:"json",
		        async:true,
			    cache:true,
		        success: function (data){
		        	$(".edit-hidden").hide();
		        	$("#contentId").val(data.fileId);
		        	$("#showContent").each(function(){
		        		var _this = $(this);
		        		getCkeditValue(_this,htmlUrl+data.fileId);
		        	});
		        	console.log(JSON.stringify(data));
		        },
		        error:function(XMLHttpRequest, textStatus, errorThrown){
		        	
		        }
			});
		}
	});
	var getCkeditValue = function(obj,url){
		if(url && url != ""){
    		$.ajax({
				url : url,
				async : true,
				dataType : 'jsonp',
				jsonp : 'jsonpCallback',// 注意此处写死jsonCallback
				success : function(data) {
					obj.html(unescape(data.result));
					var _obj = $("#gdsDesc");
					KindEditor.html(_obj,unescape(data.result));
					KindEditor.sync(_obj);
				}
			 });
		}
	};
	if($("#showContent") && $("#showContent").length > 0){
		$("#showContent").each(function(){
    		var _this = $(this);
    		getCkeditValue(_this,$(this).attr("gdsValue"));
    	});
	}
	$("body").on('change','#imgFile',function () {
        //alert("nihao");
		var path = $(this).val();
			uploadDetailImage(this, path);
	});
	var uploadDetailImage = function(object, path){
		var url =$webroot + '/upload/editorFileUpload';
		var filepath = path;
		filepath = (filepath + '').toLowerCase();
		var regex = new RegExp(
				'\\.(jpg)$|\\.(png)$|\\.(jpeg)$|\\.(gif)$', 'gi');
		/** 上传图片文件格式验证 */
		if (!filepath || !filepath.match(regex)) {
			alert('请选择图片格式为(.jpg,.png,.jpeg,.gif).');
			uploadfile.value = "";
			return;
		}
		$.ajaxFileUpload({
			url : url, // 用于文件上传的服务器端请求地址
			secureuri : false, // 一般设置为false
			fileElementId : "imgFile", // 文件上传空间的id属性 <input
			type : "POST", // get 或 post
			dataType : "JSON", // 返回值类型
			success : function(data){
				console.log(data);
				var json = JSON.parse(data)
				if(json.url){
					 $("#imgFile").replaceWith('<input type="file" id="imgFile" name="imgFile" imageId=""/>');  
					var image = '<a href="javascript:void(0)" id=\''+json.fileId+'\' ondblclick="deleteImage(\''+json.fileId+'\')" imageValue=\''+json.fileId+'\'><img src="'+json.url+'" alt="双击删除"></a>'
					$("#showImage").append(image);
				}
			}, // 服务器成功响应处理函数
			complete: function(xmlHttpRequest) {  
				
            },
			error : function(data, status, e) // 服务器响应失败处理函数
			{
				alert(e);
			}
		});
	};
	$(".edit-btn").click(function(){
		$(".edit-hidden").show();
	});
	$("#updateGdsInfo").click(function(){
		var imageValue = $("#showImage>a");
		var imageId = "";
		for(var i=0;i<imageValue.length;i++){
			var imageFileId = $(imageValue[i]).attr("imageValue");
			if(imageFileId&&imageFileId!="undefined"){
				imageId+=imageFileId+",";
			}
			
		}
		var allInput = $(".input-box").find("input");
		var obj = new Object();
		for(var i=0;i<allInput.length;i++){
			//contentHTML=contentHTML+ "<input type=\"hidden\" id=\""+$(allInput[i]).attr("id")+"\" name=\""+$(allInput[i]).attr("name")+"\" value=\""+ $(allInput[i]).val() +"\">";
			//obj.allInput[i].attr("name")=allInput[i].attr("name");
			var name = $(allInput[i]).attr("name");
			console.log(name);
			var value = $(allInput[i]).val();
			obj[name] = value;
		}
		obj.image=imageId;
		$.ajax({
			url: GLOBAL.WEBROOT+"/gds/updateGdsInfo",
			data:obj,
			type:"post",
			dataType:"json",
			async:true,
			cache:true,
			success: function (data){
				//从后台中获取到数据添加到树结构中
				if(data.flag == "success"){
					window.location.href = GLOBAL.WEBROOT+"/gds/init";
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
			}
		});
		
	});
});
function deleteImage(id){
	//var obj = this;
	$.ajax({
		url: GLOBAL.WEBROOT+"/gds/deleGdsPic",
		data:{
			image:id
		},
		type:"post",
		dataType:"json",
		async:true,
		cache:true,
		success: function (data){
			//从后台中获取到数据添加到树结构中
			if(data.flag == "success"){
				$("#"+id).remove();
			}
		},
		error:function(XMLHttpRequest, textStatus, errorThrown){
		}
	});
}